package com.booklove.controller.school;

import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.College;
import com.booklove.domain.query.CollegeQuery;
import com.booklove.result.Result;
import com.booklove.service.school.CollegeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("collegeCLL")
@RequestMapping("/colleges")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class CollegeController {

    private final CollegeService collegeService;

    /**
     * 保存新增
     */
    @PostMapping("/")
    Result<String> saveCollege(@RequestBody College collegeFormDTO) {
        College college = new College();
        BeanUtils.copyProperties(collegeFormDTO, college);
        collegeService.save(college);
        return Result.success();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteCollege(@RequestParam("id") Long id) {
        collegeService.removeById(id);
        return Result.success();

    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    Result<College> getCollege(@PathVariable Long id) {
        return Result.success(collegeService.getById(id));

    }


    /**
     * 编辑数据
     *
     * @param collegeFormDTO 实体
     * @return 编辑结果
     */
    @PutMapping("/")
    Result<String> updateCollege(@RequestBody College collegeFormDTO) {
        collegeService.updateById(collegeFormDTO);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param collegeQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<College> collegesPageQuery(CollegeQuery collegeQuery) {
        return collegeService.queryCollegesPage(collegeQuery);
    }

}

